#include "output.h"
#include "sys.h"

#define DEHUMID_TEMP    23      //默认除湿温度为23度

void output_init(void)
{
    GPIO_InitTypeDef __xdata GPIO_InitStructure;                    //结构定义
    GPIO_InitStructure.Pin = RELAY_VALVE_PIN | RELAY_WIND_HIGH_PIN | RELAY_WIND_MEDIUM_PIN | RELAY_WIND_LOW_PIN;              //指定要初始化的IO,
    GPIO_InitStructure.Mode = GPIO_OUT_PP;                  //指定IO的输入或输出方式
    GPIO_Inilize(GPIO_P2, &GPIO_InitStructure);   //初始化
    RELAY_WIND_HIGH_OFF();
    RELAY_WIND_MEDIUM_OFF();
    RELAY_WIND_LOW_OFF();
    RELAY_VALVE_OFF();
}

void valve_output(u8 isOn)
{
    static u8 __xdata preStatus;
    if(isOn != preStatus)
    {
        preStatus = isOn;
        if(isOn)
        {
            RELAY_VALVE_ON();
        }
        else
        {
            RELAY_VALVE_OFF();
        }
    }
}

void wind_output(u8 speed)
{
    static u8 __xdata ctrlStep;
    static u8 __xdata preSpeed;

    if(speed != preSpeed)
    {
        switch(ctrlStep)
        {
            case 0:
                RELAY_WIND_HIGH_OFF();
                RELAY_WIND_MEDIUM_OFF();
                RELAY_WIND_LOW_OFF();
                ctrlStep++;
            break;

            case 1:
                switch(speed)
                {
                    case OUTPUT_WIND_OFF:
                        
                    break;

                    case OUTPUT_WIND_SPEED_LOW:
                        RELAY_WIND_LOW_ON();
                    break;

                    case OUTPUT_WIND_SPEED_MEDIUM:
                        RELAY_WIND_MEDIUM_ON();
                    break;

                    case OUTPUT_WIND_SPEED_HIGH:
                        RELAY_WIND_HIGH_ON();
                    break;

                    default:
                        break;
                }
                preSpeed = speed;
                ctrlStep = 0;
            break;
            
            default:
                break;  
        }
    }
    else
    {
        switch(speed)
        {
            case OUTPUT_WIND_OFF:
                
            break;

            case OUTPUT_WIND_SPEED_LOW:
                RELAY_WIND_LOW_ON();
            break;

            case OUTPUT_WIND_SPEED_MEDIUM:
                RELAY_WIND_MEDIUM_ON();
            break;

            case OUTPUT_WIND_SPEED_HIGH:
                RELAY_WIND_HIGH_ON();
            break;

            default:
                break;
        }
    }
}

void output_airConditioner(void)
{
    static u8 __xdata coolingStep;
    static u8 __xdata heatingStep;
    static u8 __xdata timeout;      //用于作时间上的一些判断
    static u8 __xdata windDelay;    //用于风机关闭延时
    signed char __xdata _tempEk;
    static signed char __xdata preTemp;    //用于比较是否调整设定温度
    /* 空调模式开机 */
    if(sysParams.regs.powerStatus & POWER_AIR_CONDITIONER_MASK)
    {
        /* 设定温度改变 */
        if(preTemp != sysParams.regs.temperatureAirSet)
        {
            preTemp = sysParams.regs.temperatureAirSet;
            coolingStep = 0;
            heatingStep = 0;
            timeout = 0;
            windDelay = 0;
        }
        /* 空调模式 */
        switch(sysParams.regs.airConditionerMode)
        {
            /* 通风模式 */
            case AIR_CONDITIONER_MODE_AIR_CIRCULATION:
            {
                coolingStep = 0;
                heatingStep = 0;
                /* 通风模式阀关闭 */
                sysParams.regs.outputStatus &= ~OUTPUT_STATUS_AIR_CONDITIONER_MASK;
                /* 风机控制 */
                switch(sysParams.regs.airConditionerSpeedSet)
                {
                    case SET_WIND_SPEED_AUTO:
                    case SET_WIND_SPEED_LOW:
                        sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_LOW;
                    break;
                    case SET_WIND_SPEED_MEDIUM:
                        sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_MEDIUM;
                    break;
                    case SET_WIND_SPEED_HIGH:
                        sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_HIGH;
                    break;
                    default:
                        break;
                }
                /* 阀控制 */
                //通风模式不制冷
            }
            break;
            /* 制冷模式 */
            case AIR_CONDITIONER_MODE_COOLING:
            {
                heatingStep = 0;
                switch(coolingStep)
                {
                    /* 未达到平衡温度（设定温度+回差） */
                    case 0:
                    {
                        _tempEk = (signed char)sysParams.regs.temperatureSmp - sysParams.regs.temperatureAirSet;
                        /* 风机控制 */
                        switch(sysParams.regs.airConditionerSpeedSet)
                        {
                            case SET_WIND_SPEED_AUTO:
                            {
                                if(_tempEk >= 2)
                                {
                                    sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_HIGH;
                                }
                                else if(_tempEk >= 1 && _tempEk < 2)
                                {
                                    sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_MEDIUM;
                                }
                                else
                                {
                                    sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_LOW;
                                }
                            }
                            break;
                            case SET_WIND_SPEED_LOW:
                                sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_LOW;
                            break;
                            case SET_WIND_SPEED_MEDIUM:
                                sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_MEDIUM;
                            break;
                            case SET_WIND_SPEED_HIGH:
                                sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_HIGH;
                            break;
                        }
                        /* 未达到平衡温度 */
                        if(sysParams.regs.temperatureSmp > (sysParams.regs.temperatureAirSet + sysParams.regs.backlashLowerLimit))
                        {
                            /* 开启阀 */
                            sysParams.regs.outputStatus |= OUTPUT_STATUS_AIR_CONDITIONER_MASK;
                            timeout = 0;
                        }
                        else
                        {
                            timeout++;
                            /* 确保达到平衡温度 */
                            if(timeout >= 4)
                            {
                                timeout = 0;
                                /* 关闭阀 */
                                sysParams.regs.outputStatus &= ~OUTPUT_STATUS_AIR_CONDITIONER_MASK;
                                /* 风机延时关闭 */
                                windDelay = 60;
                                coolingStep++;
                            }
                        }
                    }
                    break;
                    /* 达到平衡温度 */
                    case 1:
                    {
                        /* 未达到失衡温度 */
                        if(sysParams.regs.temperatureSmp < (sysParams.regs.temperatureAirSet + sysParams.regs.backlashUpperLimit))
                        {
                            timeout = 0;
                            windDelay--;
                            /* 风机关闭延时结束 */
                            if(windDelay == 0)
                            {
                                /* 风机受控停止 */
                                if(sysParams.regs.isAirBlowerControlled)
                                {
                                    sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_OFF;
                                }
                                /* 风机不受控，转为低速 */
                                else
                                {
                                    sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_LOW;
                                }
                            }
                        }
                        /* 达到失衡温度 */
                        else
                        {
                            timeout++;
                            if(timeout >= 4)
                            {
                                timeout = 0;
                                coolingStep++;
                            }
                            
                        }
                    }
                    break;
                    /* 达到失衡温度 */
                    case 2:
                    {
                        coolingStep = 0;
                    }
                    break;
                }
            }
            break;
            /* 制热模式 */
            case AIR_CONDITIONER_MODE_HEATING:
            {
                coolingStep = 0;
                switch(heatingStep)
                {
                    /* 未达到平衡温度（设定温度+回差） */
                    case 0:
                    {
                        _tempEk = (signed char)sysParams.regs.temperatureSmp - sysParams.regs.temperatureAirSet;
                        /* 风机控制 */
                        switch(sysParams.regs.airConditionerSpeedSet)
                        {
                            case SET_WIND_SPEED_AUTO:
                            {
                                if(_tempEk <= -2)
                                {
                                    sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_HIGH;
                                }
                                else if(_tempEk <= -1 && _tempEk > -2)
                                {
                                    sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_MEDIUM;
                                }
                                else
                                {
                                    sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_LOW;
                                }
                            }
                            break;
                            case SET_WIND_SPEED_LOW:
                                sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_LOW;
                            break;
                            case SET_WIND_SPEED_MEDIUM:
                                sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_MEDIUM;
                            break;
                            case SET_WIND_SPEED_HIGH:
                                sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_HIGH;
                            break;
                        }
                        /* 未达到平衡温度 */
                        if(sysParams.regs.temperatureSmp < (sysParams.regs.temperatureAirSet + sysParams.regs.backlashUpperLimit))
                        {
                            timeout = 0; 
                            /* 开启阀 */
                            sysParams.regs.outputStatus |= OUTPUT_STATUS_AIR_CONDITIONER_MASK;
                        }
                        else
                        {
                            timeout++;
                            /* 确保达到平衡温度 */
                            if(timeout >=4)
                            {
                                timeout = 0;
                                /* 关闭阀 */
                                sysParams.regs.outputStatus &= ~OUTPUT_STATUS_AIR_CONDITIONER_MASK;
                                /* 风机延时关闭 */
                                windDelay = 60;
                                heatingStep++;
                            }
                        }
                    }
                    break;
                    /* 达到平衡温度 */
                    case 1:
                    {
                        _tempEk = (signed char)sysParams.regs.temperatureSmp - (sysParams.regs.temperatureAirSet + sysParams.regs.backlashLowerLimit);
                        /* 未达到失衡温度 */
                        if(sysParams.regs.temperatureSmp > (sysParams.regs.temperatureAirSet + sysParams.regs.backlashLowerLimit))
                        {
                            windDelay--;
                            /* 风机关闭延时结束 */
                            if(windDelay == 0)
                            {
                                /* 风机受控停止 */
                                if(sysParams.regs.isAirBlowerControlled)
                                {
                                    sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_OFF;
                                }
                                /* 风机不受控，转为低速 */
                                else
                                {
                                    sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_LOW;
                                }
                            }
                        }
                        /* 达到失衡温度 */
                        else
                        {
                            timeout++;
                            if(timeout >= 4)
                            {
                                timeout = 0;
                                heatingStep++;
                            }
                            
                        }
                    }
                    break;
                    /* 达到失衡温度 */
                    case 2:
                    {
                        heatingStep = 0;
                    }
                    break;
                }
            }
            break;
            /* 除湿模式 */
            case AIR_CONDITIONER_MODE_DEHUMIDIFICATION:
            {
                coolingStep = 0;
                heatingStep = 0;
                /* 除湿不作逻辑控制 */
            }
            break;
            default:
                break;
        }
    }
    /* 空调模式关机 */
    else
    {
        /* 常规模式 */
        if(sysParams.regs.isLowTemperatureProtected == 0)
        {
            coolingStep = 0;
            heatingStep = 0;
            timeout = 0;
            windDelay = 0;
            sysParams.regs.outputStatus &= ~OUTPUT_STATUS_AIR_CONDITIONER_MASK;
            sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_OFF;
        }
        /* 低温保护模式 */
        else
        {
                /* 制热模式 */
                if(sysParams.regs.airConditionerMode == AIR_CONDITIONER_MODE_HEATING)
                {
                    /* 温度低于3度 */
                    if(sysParams.regs.temperatureSmp < 3)
                    {
                        sysParams.regs.outputStatus |= OUTPUT_STATUS_AIR_CONDITIONER_MASK;
                        sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_SPEED_LOW;
                    }
                    /* 温度达到5度 */
                    else if(sysParams.regs.temperatureSmp >= 5)
                    {
                        sysParams.regs.outputStatus &= ~OUTPUT_STATUS_AIR_CONDITIONER_MASK;
                        sysParams.regs.airConditionerSpeedOut = OUTPUT_WIND_OFF;
                    }
                }
        }
    }
    /* 输出控制 */
    if(sysParams.relayMode == RELAY_MODE_AIR_CONDITIONER)
    {
        if(sysParams.regs.outputStatus & OUTPUT_STATUS_AIR_CONDITIONER_MASK)
            valve_output(1);
        else
            valve_output(0);
        wind_output(sysParams.regs.airConditionerSpeedOut);
    }
}

void output_floorHeating(void)
{
    static u8 __xdata step;
    signed char __xdata _tempEk;
    static u8 __xdata timeout;
    static signed char __xdata preTemp;    //用于比较是否调整设定温度
    /* 地暖模式开机 */
    if(sysParams.regs.powerStatus & POWER_FLOOR_HEATING_MASK)
    {
        if(preTemp != sysParams.regs.temperatureFloorSet)
        {
            preTemp = sysParams.regs.temperatureFloorSet;
            timeout = 0;
            step = 0;
        }
        switch(step)
        {
            /* 未达到平衡温度（设定温度+回差） */
            case 0:
            {
                _tempEk = (signed char)sysParams.regs.temperatureSmp - (sysParams.regs.temperatureFloorSet + sysParams.regs.backlashUpperLimit);
                /* 未达到平衡温度 */ 
                if(_tempEk < 0)
                {
                    timeout = 0;
                    /* 开启阀 */
                    sysParams.regs.outputStatus |= OUTPUT_STATUS_FLOOR_HEATING_MASK;
                }
                else
                {
                    timeout++;
                    /* 确保达到平衡温度 */
                    if(timeout >=4)
                    {
                        timeout = 0;
                        /* 关闭阀 */
                        sysParams.regs.outputStatus &= ~OUTPUT_STATUS_FLOOR_HEATING_MASK;

                        step++;
                    }
                }
            }
            break;
            /* 达到平衡温度 */
            case 1:
            {
                _tempEk = (signed char)sysParams.regs.temperatureSmp - (sysParams.regs.temperatureFloorSet + sysParams.regs.backlashLowerLimit);
                /* 未达到失衡温度 */
                if(_tempEk > 0)
                {
                    /* 不作任何处理 */
                }
                /* 达到失衡温度 */
                else
                {
                    timeout++;
                    if(timeout >= 4)
                    {
                        timeout = 0;
                        step++;
                    }
                    
                }
            }
            break;
            /* 达到失衡温度 */
            case 2:
            {
                step = 0;
            }
            break;
        }
    }
    /* 地暖模式关机 */
    else
    {
        /* 常规模式 */
        if(sysParams.regs.isLowTemperatureProtected == 0)
        {
            /* 关闭阀 */
            sysParams.regs.outputStatus &= ~OUTPUT_STATUS_FLOOR_HEATING_MASK;
            step = 0;
            timeout = 0;
        }
        /* 低温保护模式 */
        else
        {
            /* 温度低于3度 */
            if(sysParams.regs.temperatureSmp < 3)
            {
                sysParams.regs.outputStatus |= OUTPUT_STATUS_FLOOR_HEATING_MASK;
            }
            /* 温度达到5度 */
            else if(sysParams.regs.temperatureSmp >= 5)
            {
                sysParams.regs.outputStatus &= ~OUTPUT_STATUS_FLOOR_HEATING_MASK;
            }
        }
    }
    /* 输出控制 */
    if(sysParams.relayMode == RELAY_MODE_FRESH_AIR_FLOOR_HEATING)
    {
        if(sysParams.regs.outputStatus & OUTPUT_STATUS_FLOOR_HEATING_MASK)
            valve_output(1);
        else
            valve_output(0);
    }
}

void output_freshAir(void)
{
    if(sysParams.regs.powerStatus & POWER_FRESH_AIR_MASK)
    {
        /* 风机控制 */
        switch(sysParams.regs.freshAirSpeedSet)
        {
            case SET_WIND_SPEED_LOW:
                sysParams.regs.freshAirSpeedOut = OUTPUT_WIND_SPEED_LOW;
            break;
            case SET_WIND_SPEED_MEDIUM:
                sysParams.regs.freshAirSpeedOut = OUTPUT_WIND_SPEED_MEDIUM;
            break;
            case SET_WIND_SPEED_HIGH:
                sysParams.regs.freshAirSpeedOut = OUTPUT_WIND_SPEED_HIGH;
            break;
        }
    }
    /* 新风模式关机 */
    else
    {
        sysParams.regs.freshAirSpeedOut = OUTPUT_WIND_OFF;
    }
    /* 输出控制 */
    if(sysParams.relayMode == RELAY_MODE_FRESH_AIR_FLOOR_HEATING)
    {
        wind_output(sysParams.regs.freshAirSpeedOut);
    }
}

void output_func(void)
{
    /* 空调输出 */
    output_airConditioner();
    /* 地暖输出 */
    output_floorHeating();
    /* 新风输出 */
    output_freshAir();
}